package com.xju.ikun.controller;

import com.xju.ikun.bean.*;
import com.xju.ikun.controller.common.CommonResponse;
import com.xju.ikun.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/music")
public class MusicController {
    @Autowired
    MtypeService mtypeService;
    @Autowired
    SongerService songerService;
    @Autowired
    SongService songService;
    @Autowired
    ListsService listsService;
    @Autowired
    UserService userService;
    @Autowired
    LbimgService lbimgService;
    @Autowired
    AlbumService albumService;
    @Autowired
    UserlistService userlistService;
    @Autowired
    SonglistService songlistService;
    @Autowired
    SongerlistService songerlistService;
    @Autowired
    MsrListService msrListService;

    @GetMapping("/mtype")
    public CommonResponse getMytypeBean(){//查询所有mtype
        List<MtypeBean> mtypeBean = mtypeService.findAll();
        CommonResponse <List<MtypeBean>> response = CommonResponse.createForSuccess("查询成功！", mtypeBean);
        return response;
    }

    @RequestMapping("/insertmtype")//插入mtype
    public CommonResponse insertMtype(String Tname,String Tdesc){
        MtypeBean mtypeBean=new MtypeBean();
        mtypeBean.setTname(Tname);
        mtypeBean.setTdesc(Tdesc);
        int a = mtypeService.insertMtypeBean(mtypeBean);
        if (a==1){
            CommonResponse<UserBean> response = CommonResponse.createForSuccessMessage("插入成功！");
            return response;
        }
        else{
            return CommonResponse.createForError("插入失败！");
        }
    }
    @GetMapping("/findSonger")//歌手的模糊查询
    public CommonResponse getSongerBean(String name){
        List<SongerBean> songerBean = songerService.selectLikeOne("%"+name+"%");
        CommonResponse <List<SongerBean>> response = CommonResponse.createForSuccess("查询成功！", songerBean);
        return response;
    }
    @GetMapping("/findSong")//歌曲的模糊查询
    public CommonResponse getSongBean(String sname){
        List<SongBean> songBean = songService.selectLikeOne("%"+sname+"%");
        CommonResponse <List<SongBean>> response = CommonResponse.createForSuccess("查询成功！", songBean);
        return response;
    }

    @GetMapping("/findUserList")//查找用户有哪些歌单
    public CommonResponse getUserListBean(int userid){
        List<ListsBean> listsBeans = listsService.findByUserid(userid);
        CommonResponse <List<ListsBean>> response = CommonResponse.createForSuccess("查询成功！", listsBeans);
        return response;
    }
    @GetMapping("/findListSong")//查找歌单有哪些歌
    public CommonResponse getListSongBean(int listid){
        List<SongBean> songBeans = songService.findByListid(listid);
        CommonResponse <List<SongBean>> response = CommonResponse.createForSuccess("查询成功！", songBeans);
        return response;
    }
    @GetMapping("/findUserSonger")//查找用户有哪些关注的明星
    public CommonResponse getUserSongerBean(int userid){
        List<SongerBean> songerBeans = songerService.findByUserid(userid);
        CommonResponse <List<SongerBean>> response = CommonResponse.createForSuccess("查询成功！", songerBeans);
        return response;
    }

    @GetMapping("/findUserByUserid")//根据用户id查询用户
    public CommonResponse getUserBean(int userid){
        UserBean userBean = userService.findByUserid(userid);
        CommonResponse <UserBean> response = CommonResponse.createForSuccess("查询成功！", userBean);
        return response;
    }

    @GetMapping("/findAllLbimg")//根据用户id查询用户
    public CommonResponse getUserBean(){
        List<LbimgBean> lbimgBean = lbimgService.findAll();
        CommonResponse <List<LbimgBean>> response = CommonResponse.createForSuccess("查询成功！", lbimgBean);
        return response;
    }

    @GetMapping("/findAllAlbum")//根据用户id查询用户
    public CommonResponse getAlbumBean(){
        List<AlbumBean> albumBeans = albumService.findAll();
        CommonResponse <List<AlbumBean>> response = CommonResponse.createForSuccess("查询成功！", albumBeans);
        return response;
    }

    @GetMapping("/findUserlistBean")//查询用户是否收藏了这个歌单
    public CommonResponse getUserlistBean(int userid,int listid){
        UserlistBean userlistBean = userlistService.findByUseridListid(userid,listid);
        if(userlistBean != null) {
            CommonResponse<UserlistBean> response = CommonResponse.createForSuccess("查询成功！", userlistBean);
            return response;
        } else {
            return CommonResponse.createForError("查询失败！");
        }
    }

    @GetMapping("/findSonglistBean")//查询歌单里面是否有这首歌
    public CommonResponse getSonglistBean(int listid,int songid){
        SonglistBean songlistBean = songlistService.findByListidSongid(listid,songid);
        if(songlistBean != null) {
            CommonResponse<SonglistBean> response = CommonResponse.createForSuccess("查询成功！", songlistBean);
            return response;
        } else {
            return CommonResponse.createForError("查询失败！");
        }
    }

    @GetMapping("/findSongerlistBean")//查询用户是否关注了这个歌手
    public CommonResponse getSongerlistBean(int userid,int songerid){
        SongerlistBean songerlistBean = songerlistService.findByUseridSongerid(userid,songerid);
        if(songerlistBean != null) {
            CommonResponse<SongerlistBean> response = CommonResponse.createForSuccess("查询成功！", songerlistBean);
            return response;
        } else {
            return CommonResponse.createForError("查询失败！");
        }
    }

    @RequestMapping("/updateUserBean")//更新User
    public CommonResponse updateUser(int userid,String username,String qq,String email,String nick,String tel){
        UserBean userBean=new UserBean();
        userBean.setUser_id(userid);
        userBean.setUsername(username);
        userBean.setQq(qq);
        userBean.setEmail(email);
        userBean.setNick(nick);
        userBean.setTel(tel);
        int a = userService.updateByPrimaryKey(userBean.getUser_id(), userBean.getUsername(), userBean.getQq(),userBean.getEmail(),userBean.getNick(),userBean.getTel());
        if (a==1){
            CommonResponse<UserBean> response = CommonResponse.createForSuccessMessage("更新成功！");
            return response;
        }
        else{
            return CommonResponse.createForError("更新失败！");
        }
    }
    @GetMapping("/findAllUser")//根据用户id查询用户
    public CommonResponse getAllUserBean(){
        List<UserBean> userBean = userService.findAll();
        CommonResponse <List<UserBean>> response = CommonResponse.createForSuccess("查询成功！", userBean);
        return response;
    }
    @GetMapping("/findSongBeanbyAlbumid")//根据专辑id查找歌曲
    public CommonResponse getAllUserBean(int aid){
        List<SongBean> songBeans = songService.findByAlbumid(aid);
        CommonResponse <List<SongBean>> response = CommonResponse.createForSuccess("查询成功！", songBeans);
        return response;
    }
    @GetMapping("/findAlbumBeanbyAlbumid")//根据唱片id查询唱片
    public CommonResponse getAlbumBean(int aid){
        AlbumBean albumBean = albumService.findByAlbumid(aid);
        CommonResponse <AlbumBean> response = CommonResponse.createForSuccess("查询成功！", albumBean);
        return response;
    }
    @RequestMapping("/deleteUserBean")//删除用户
    public CommonResponse deleteUserBean(int userid){
        int a = userService.deleteByPrimaryKey(userid);
        if (a==1){
            CommonResponse<UserBean> response = CommonResponse.createForSuccessMessage("删除成功！");
            return response;
        }
        else{
            return CommonResponse.createForError("删除失败！");
        }
    }
    @GetMapping("/findAllSongBean")//查询所有歌手
    public CommonResponse getAllSongBean(){
        List<SongBean> songBeans = songService.findAll();
        CommonResponse <List<SongBean>> response = CommonResponse.createForSuccess("查询成功！", songBeans);
        return response;
    }
    @RequestMapping("/insertSongBean")//插入新的歌曲
    public CommonResponse insertSongBean(String sname){
        SongBean songBean=new SongBean();
        songBean.setSname(sname);
        int a = songService.insertSongBean(songBean);
        if (a==1){
            CommonResponse<SongBean> response = CommonResponse.createForSuccessMessage("插入成功！");
            return response;
        }
        else{
            return CommonResponse.createForError("插入失败！");
        }
    }
    @RequestMapping("/deteleSongBean")//删除新的歌曲
    public CommonResponse deteleSongBean(int sid){
        int a = songService.deleteByPrimaryKey(sid);
        if (a==1){
            CommonResponse<SongBean> response = CommonResponse.createForSuccessMessage("删除成功！");
            return response;
        }
        else{
            return CommonResponse.createForError("删除失败！");
        }
    }

    @RequestMapping("/updateSongBean")//更新User
    public CommonResponse updateSongBean(int sid,String sname){
        SongBean songBean = new SongBean();
        songBean.setSid(sid);
        songBean.setSname(sname);
        int a = songService.updateByPrimaryKey(songBean);
        if (a==1){
            CommonResponse<SongBean> response = CommonResponse.createForSuccessMessage("更新成功！");
            return response;
        }
        else{
            return CommonResponse.createForError("更新失败！");
        }
    }
    @GetMapping("/findAllSongerBean")//查询所有歌手
    public CommonResponse getAllSongerBean(){
        List<SongerBean> songerBeans = songerService.findAll();
        CommonResponse <List<SongerBean>> response = CommonResponse.createForSuccess("查询成功！", songerBeans);
        return response;
    }
    @RequestMapping("/insertSongerBean")//插入新的歌手
    public CommonResponse insertSongerBean(String srname){
        SongerBean songerBean=new SongerBean();
        songerBean.setSrname(srname);
        int a = songerService.insertSongerBean(songerBean);
        if (a==1){
            CommonResponse<SongerBean> response = CommonResponse.createForSuccessMessage("插入成功！");
            return response;
        }
        else{
            return CommonResponse.createForError("插入失败！");
        }
    }
    @RequestMapping("/deteleSongerBean")//删除歌手
    public CommonResponse deteleSongerBean(int srid){
        int a = songerService.deleteByPrimaryKey(srid);
        if (a==1){
            CommonResponse<SongBean> response = CommonResponse.createForSuccessMessage("删除成功！");
            return response;
        }
        else{
            return CommonResponse.createForError("删除失败！");
        }
    }

    @RequestMapping("/updateSongerBean")//更新歌手信息
    public CommonResponse updateSongerBean(int srid,String srname){
        SongerBean songerBean = new SongerBean();
        songerBean.setSrid(srid);
        songerBean.setSrname(srname);
        int a = songerService.updateByPrimaryKey(songerBean);
        if (a==1){
            CommonResponse<SongBean> response = CommonResponse.createForSuccessMessage("更新成功！");
            return response;
        }
        else{
            return CommonResponse.createForError("更新失败！");
        }
    }
    @RequestMapping("/insertMsrListBean")//插入我喜欢歌单
    public CommonResponse insertMsrListBean(int user_id,int songid){
        MsrListBean msrListBean=new MsrListBean();
        msrListBean.setMrslist_mrlistid(user_id);
        msrListBean.setMrslist_songid(songid);
        int a = msrListService.insertinsertMsrListBean(msrListBean);
        if (a==1){
            CommonResponse<UserBean> response = CommonResponse.createForSuccessMessage("插入成功！");
            return response;
        }
        else{
            return CommonResponse.createForError("插入失败！");
        }

    }
    @GetMapping("/findMrsListSong")//查找我喜欢歌单有哪些歌
    public CommonResponse getMrsListSongBean(int user_id){
        List<SongBean> songBeans = songService.findByUserid(user_id);
        CommonResponse <List<SongBean>> response = CommonResponse.createForSuccess("查询成功！", songBeans);
        return response;
    }

    @RequestMapping("/deleteByMrListidAndSongid")//删除歌手
    public CommonResponse deteleMsrListBean(int user_id,int songid){
        int a = msrListService.deleteByMrListidAndSongid(user_id,songid);
        if (a==1){
            CommonResponse<SongBean> response = CommonResponse.createForSuccessMessage("删除成功！");
            return response;
        }
        else{
            return CommonResponse.createForError("删除失败！");
        }
    }

}
